#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年10月19日

@author: chenyongbing
'''
import sys,os
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

'''
    获取 安卓、ios(包含随机mac) 顾客数量占比
'''


sys.path.append(os.path.join(current_dir,'../../'))
from base.retail_datas import myRetailDatas
from base.zhimind_datas import myZhimindDatas


def analysis_android_and_ios(st=None,et=None,group=None):
    fr = open(os.path.join(current_dir,'../log/analysis_android_and_ios.txt'),'w')
    androidDatas  = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group(distinct=True, group=group, st=st, et=et, dis_factory='Apple', mac_type='global')
    
    allDatas = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group(distinct=True,group=group,  st=st, et=et, mac_type='all')
    nolocalDatas = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group(distinct=True,group=group,  st=st, et=et, mac_type='global')
    print '\t'.join([u'group',u'门店名',u'公司',u'总数',u'安卓顾客数',u'安卓顾客占比',u'安卓+苹果(不包含随机mac)',u'安卓+苹果占比'])
    fr.write('\t'.join(['group','门店名','公司','总数','安卓顾客数','安卓顾客占比','安卓+苹果(不包含随机mac)','安卓+苹果占比'])+'\n')
    for group , total_count in allDatas.items():
        group_name = myRetailDatas.get_group_name(group=group).encode('utf-8')
        comp_name = myRetailDatas.get_comp_name(shop_group=group).encode('utf-8')
        if group not in androidDatas.keys() and group not in nolocalDatas.keys():
            android_count = '-'
            android_radio = '-'
            nolocal_count = '-'
            nolocal_radio = '-'
        elif group not in androidDatas.keys():
            android_count = '-'
            android_radio = '-'
            nolocal_count = nolocalDatas[group]
            nolocal_radio = round(float(nolocal_count)/total_count,2)
        elif group not in nolocalDatas.keys():
            android_count = androidDatas[group]
            android_radio = round(float(android_count)/total_count,2)
            nolocal_count = '-'
            nolocal_radio = '-'
        else:
            android_count = androidDatas[group]
            android_radio = round(float(android_count)/total_count,2)
            nolocal_count = nolocalDatas[group]
            nolocal_radio = round(float(nolocal_count)/total_count,2)
        print '\t'.join([str(group),group_name,comp_name,str(total_count),str(android_count),str(android_radio),str(nolocal_count),str(nolocal_radio)])
        fr.write('\t'.join([str(group),group_name,comp_name,str(total_count),str(android_count),str(android_radio),str(nolocal_count),str(nolocal_radio)])+'\n')
    fr.close()


if __name__== '__main__':
    sys.path.append(os.path.join(current_dir,'../'))
    from base.mylogging import *
    import argparse,re,datetime,time
    
    parser = argparse.ArgumentParser(description='encrypt customer upload file')
    parser.add_argument('--st',metavar="st",default=None)
    parser.add_argument('--et',metavar="st",default=None)
    parser.add_argument('--group',metavar='group ',default=None)

    args = parser.parse_args()
    
    st = args.st
    et = args.et
    group = args.group
    
    if st ==None:
        st = datetime.date.today().strftime("%Y-%m-%d")
    if et == None:
        et = datetime.date.today().strftime("%Y-%m-%d")

    analysis_android_and_ios(st=st,et=et,group=group)
